package com.seekting.mynews.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.FitCenter;
import com.seekting.mynews.R;
import com.seekting.mynews.connect.tiangou.bean.response.TianGouGallery;

import java.util.List;

/**
 * Created by seekting on 2016/6/28.
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.TitleViewHolder> implements View.OnClickListener {
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<TianGouGallery> mList;
    private String begin;
    private final RequestManager mRequestManager;
    private int width;

    public void setOnRecycleItemClickListener(OnRecycleItemClickListener<TianGouGallery> onRecycleItemClickListener) {
        mOnRecycleItemClickListener = onRecycleItemClickListener;
    }

    OnRecycleItemClickListener<TianGouGallery> mOnRecycleItemClickListener;


    public ImageAdapter(Context context, String begin, List<TianGouGallery> list, RequestManager requestManager) {
        this.mContext = context;
        mRequestManager = requestManager;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.begin = begin;
        this.mList = list;
        width = context.getResources().getDisplayMetrics().widthPixels / 3;
    }

    @Override
    public ImageAdapter.TitleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = mLayoutInflater.inflate(R.layout.list_item, null);
        TitleViewHolder titleViewHolder = new TitleViewHolder(convertView);
        convertView.setOnClickListener(this);
        titleViewHolder.networkImageView.setLayoutParams(new FrameLayout.LayoutParams(width, width));
        return titleViewHolder;
    }

    @Override
    public void onBindViewHolder(ImageAdapter.TitleViewHolder holder, int position) {
        TianGouGallery tainGallery = mList.get(position);
//        holder.networkImageView.setDefaultImageResId(R.drawable.default_img);
        String url = begin + tainGallery.getImg();
//        holder.networkImageView.setImageUrl(url, mImageLoader);
        mRequestManager.load(url).override(width, width).dontAnimate().transform(new FitCenter(mContext)).placeholder(R.drawable.default_img).into(holder.networkImageView);
        holder.position = position;
        holder.mTianGouGallery = tainGallery;
        holder.itemView.setTag(holder);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnRecycleItemClickListener != null) {
            TitleViewHolder holder = (TitleViewHolder) v.getTag();
            mOnRecycleItemClickListener.onItemClick(holder.position, holder.mTianGouGallery);
        }
    }

    public static class TitleViewHolder extends RecyclerView.ViewHolder {
        ImageView networkImageView;
        int position;
        TianGouGallery mTianGouGallery;

        public TitleViewHolder(View itemView) {
            super(itemView);
            networkImageView = (ImageView) itemView.findViewById(R.id.img);

        }
    }
}
